package com.wgke.bluetooth.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.newmobiler.android.ui.adapter.CellAdapter;
import com.newmobiler.android.ui.adapter.cell.MultiCell;
import com.newmobiler.android.ui.adapter.divider.RVItemDivider;
import com.wgke.bluetooth.R;

import java.util.Arrays;
import java.util.stream.Collectors;

public class ChooseDialog extends BaseDialog {
    int[] arr;
    String doc;

    int index = 0;
    String value = "0";
    String[] names;

    public ChooseDialog(@NonNull Context context, int[] arr, String doc) {
        super(context);
        this.arr = arr;
        this.doc = doc;
    }


    public ChooseDialog(@NonNull Context context, int[] arr, String[] names, String doc) {
        super(context);
        this.names = names;
        this.arr = arr;
        this.doc = doc;
    }

    public ChooseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public int layoutId() {
        return R.layout.dialog_choose;
    }

    @Override
    public void initView() {
        findViewById(R.id.tvTitleChoose).setVisibility(TextUtils.isEmpty(doc) ? View.GONE : View.VISIBLE);
        ((TextView) findViewById(R.id.tvTitleChoose)).setText("" + doc);
        TextView button = findViewById(R.id.tv_confirm);
        WheelView wheelView = findViewById(R.id.wheelView);

        if (arr.length > 4) {
            findViewById(R.id.rvChoose).setVisibility(View.GONE);
            wheelView.setAdapter(new ArrayWheelAdapter(Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.toList())));
            wheelView.setCurrentItem(2); // 默认选中第三项

            wheelView.setOnItemSelectedListener(index -> {
                this.index = index;
                this.value = "" + arr[index];
            });
            wheelView.setTextSize(20f); // 单位px
            wheelView.setCyclic(true);
            wheelView.setLineSpacingMultiplier(2.5f);
        } else {
            wheelView.setVisibility(View.GONE);
            RecyclerView rv = findViewById(R.id.rvChoose);
            CellAdapter cellAdapter = CellAdapter.createAdapter(rv);
            if (names != null && names.length == arr.length) {
                cellAdapter.setDataList(MultiCell.convert2(R.layout.item_tv, Arrays.stream(names).collect(Collectors.toList()), (h, c) -> {
                    h.setText(R.id.tvBtn, c);
                    h.getView().setOnClickListener(v -> {
                        if (callback != null)
                            callback.chooseIndex(h.getAdapterPosition(), arr[h.getAdapterPosition()]);
                        dismiss();
                    });
                }));
            } else {
                cellAdapter.setDataList(MultiCell.convert2(R.layout.item_tv, Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.toList()), (h, c) -> {
                    h.setText(R.id.tvBtn, c);
                    h.getView().setOnClickListener(v -> {
                        if (callback != null)
                            callback.chooseIndex(h.getAdapterPosition(), Integer.parseInt(c));
                        dismiss();
                    });
                }));
            }

            rv.addItemDecoration(new RVItemDivider(R.color.black_ccc, 1));
            button.setText("取消");

        }
        button.setOnClickListener(v -> {
            if (arr.length > 4 && callback != null) {
                callback.chooseIndex(index, Integer.parseInt(value));
            }
            dismiss();
        });
    }
}
